;(function() {
	'use strict';
	angular.module('services.faultLocation.analysisServer', [])
		.factory('analysisServer', analysisServer)
		.service('analysisData', AnalysisData);

	function analysisServer(Restangular, analysisData, CoreService, odOperationDevelop,CoreLoading) {
		var year = new Date().getFullYear();
		var month = new Date().getMonth();
		var day = new Date().getDate();
		return {
			getStandardType: getStandardType,
			queryAlarmLevel: queryAlarmLevel,
			getPageBind: getPageBind,
			queryComparedNum: queryComparedNum,
			fmtSendData: fmtSendData
		};

		function getStandardType() {
			return [{
				name: '同比',
				code: '1'
			}, {
				name: '环比',
				code: '2'
			}, {
				name: '定基比',
				code: '3'
			}];
		}

		function queryAlarmLevel(params) {
			Restangular.all('sas/faultLocation/appAlarms/alarmLevelQuery').post(params).then(function(res) {
				return analysisData.DATA = res.data;
			});
			return analysisData;
		}

		function getPageBind() {
			return {
				systemCode: '11',
				timeType: '1',
				startTime: new Date(year, month, day),
				endTime: new Date(year, month, day),
				alarmLevelCode: '',
				standardType: '同比',
				msg: ''
			};
		}

		function fmtSendData(sendData) {
			var bind = {
				systemCode: '11',
				timeType: '1',
				startTime: new Date(year, month, day),
				endTime: new Date(year, month, day),
				msg: '',
				alarmLevelCode: [],
				alarmLevelName: [],
				page: {
					currentPage: 1,
					pageSize: '10'
				},
				standardTypeName: '同比'
			};
			//格式化时间
			if (sendData.timeType === '0') {
				bind.startTime = CoreService.dateFormat(sendData.startTime, 'yyyy-MM-dd hh') + ':00:00';
				bind.endTime = CoreService.dateFormat(sendData.endTime, 'yyyy-MM-dd hh') + ':00:00';
			} else {
				bind.startTime = CoreService.dateFormat(sendData.startTime, 'yyyy-MM-dd') + ' 00:00:00';
				bind.endTime = CoreService.dateFormat(sendData.endTime, 'yyyy-MM-dd') + ' 00:00:00';
			}
			if (sendData.alarmLevelCode !== '') {
				bind.alarmLevelCode.push(sendData.alarmLevelCode);
				bind.alarmLevelName.push(sendData.alarmLevelName);
			}
			bind.timeType = sendData.timeType;
			bind.systemCode = sendData.systemCode;
			bind.standardTypeName = sendData.standardType;
			return bind;
		}
		//查询应用告警数
		function queryComparedNum(params,flag) {
			Restangular.all('sas/faultLocation/appAlarms/queryStrainAppAlarmNum').post(params).then(function(res) {
				CoreLoading.setState(false);
				if(res.data.data.length>0){
				res.state === '10000' ? getComparedEchartsData(params, res.data.data) : CoreService.dialog('查询错误');
					flag = 'view';
				}else{
					CoreService.dialog('查询无数据');
					flag = 'v';
					//return;
				}
			});
		}

		function getComparedEchartsData(send, items) {
			var myEcharts = echarts.init(document.getElementById('doc'));
			var temp = {
				echartsXData: [],
				title:'',
				level:'',
				systemName:''
			};
			angular.forEach(items, function(item) {
				temp.level = item.alarmLevelName;
				temp.systemName = item.systemName;
				if (temp.echartsXData.indexOf(item.time)) {
					temp.echartsXData.push(item.time);
				}
			});
			var title = temp.systemName + temp.level + send.standardTypeName+ '统计';
			if (temp.echartsXData[0] === temp.echartsXData[temp.echartsXData.length - 1]) {
				temp.title = temp.echartsXData[0].replace(/-/g, '') + title;
			} else {
				temp.title = temp.echartsXData[0].replace(/-/g, '') + ' 到 ' + temp.echartsXData[temp.echartsXData.length - 1].replace(/-/g, '') + title;
			}

			var option = odOperationDevelop.getEchartsData(items,temp.title, 'time', 'standardTypeName', 'alarmNum', 'bar');
			/*option.series.name=send.standardTypeName;*/
			//debugger;
			angular.forEach(option.series, function(seriesData) {
				/*debugger;
				seriesData.name=send.standardTypeName;*/
				if (seriesData.name === '同比') {
					seriesData.type = 'line';
				}
			});
			//console.log(option);
			myEcharts.setOption(option);
		}

	}

	function AnalysisData() {
		this.DATA = [];
	}

})();